<?php

class customer_model extends jqxGrid_CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    function get($ID){
        $where=array('ID'=>$ID);
        $query=$this->db->get_where("dtb_customer", $where); 
        return $query->result();
    }
    function getByGroup($group="-100"){
        if($group!="-100"){
            $where['`dtb_mail`.`Type`']=$group;
        }
        $where['`dtb_mail`.`Delete`']=NULL;
        $query=$this->db
                ->select("`dtb_mail`.`Email`,`dtb_mail`.`LastSend`,`dtb_customer`.`CustomerName`")
                ->join("`dtb_customer`","dtb_customer.Mail = dtb_mail.Email","left")
                ->where($where)
                ->group_by("`dtb_mail`.`Email`")
                ->get("`dtb_mail`"); 
        //echo $this->db->last_query();
        return $query->result();
    }
    function insert($params){
        $this->db->set('Insert', 'NOW()', FALSE);
        $this->db->insert("dtb_customer", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function delete($ID){
        $this->db->set('Delete', 'NOW()', FALSE);
        $where=array('ID'=>$ID);
        $this->db->where($where);
        $this->db->update("dtb_customer"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function retore($ID){
        $this->db->set('Delete', 'NULL', FALSE);
        $where=array('ID'=>$ID);
        $this->db->where($where);
        $this->db->update("dtb_customer"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($ID,$params){
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->where('ID', $ID);
        $this->db->update("dtb_customer", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function updateGroupMail($ID,$Group){
        $this->db
                ->set("`dtb_mail`.`Type`",$Group)
                ->where("`dtb_customer`.`ID`",$ID)
                ->where("`dtb_mail`.`Email`=`dtb_customer`.`Mail`")
                ->update('`dtb_mail`,`dtb_customer`'); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function updateGroupMailInList($ID,$Group){
        $this->db
                ->set("`dtb_mail`.`Type`",$Group)
                ->where_in("`dtb_customer`.`ID`",$ID)
                ->where("`dtb_mail`.`Email`=`dtb_customer`.`Mail`")
                ->update('`dtb_mail`,`dtb_customer`'); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function updateInList($ID,$params){
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->where_in('ID', $ID);
        $this->db->update("dtb_customer", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function updateSendMail($aEmail){
        $this->db->set('LastSend', 'NOW()', FALSE);
        $this->db->where_in('Email', $aEmail);
        $this->db->update("dtb_mail"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function binding(){
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS dtb_customer.*,`tbl_type`.`Name` as GroupName
            FROM `dtb_customer`
                LEFT JOIN `tbl_type` ON ( `dtb_customer`.`Group` = `tbl_type`.`Keyword` AND `tbl_type`.`Type`='Group')
            ";
        $strWhere="
            WHERE  true
            ";
        if($_SESSION["ADP"][CCTRL]["display"]==0){
            $strWhere.=" AND `dtb_customer`.`Delete` IS NULL";
        }elseif($_SESSION["ADP"][CCTRL]["display"]==-1){
            $strWhere.=" AND `dtb_customer`.`Delete` IS NOT NULL";
        }
        $strOrderBy="Order By dtb_customer.`Insert` DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>true,
            "fields"=>array(
                "ID"=>"`CustomerID`",
                "Type"=>"`dtb_customer`.`Type`",
                "GroupName"=>"`tbl_type`.`Name`",
                "Insert"=>"`dtb_customer`.`Insert`",
                "Update"=>"`dtb_customer`.`Update`",
                "Delete"=>"`dtb_customer`.`Delete`"
            ),
            "datefields"=>array("Insert","Update","Delete")
        );
        $this->init($config);
        return $this->jqxBinding();
    }
    function bindingMail(){
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `dtb_mail`.*,`dtb_mail`.`id` as 'ID',`tbl_type`.`Name` as GroupName
            FROM `dtb_mail`
                LEFT JOIN `tbl_type` ON ( `dtb_mail`.`Type` = `tbl_type`.`Keyword` AND `tbl_type`.`Type`='Group')
            ";
        $strWhere="
            WHERE  true
            ";
        if($_SESSION["ADP"][CCTRL]["display"]==0){
            $strWhere.=" AND `dtb_mail`.`Delete` IS NULL";
        }elseif($_SESSION["ADP"][CCTRL]["display"]==-1){
            $strWhere.=" AND `dtb_mail`.`Delete` IS NOT NULL";
        }
        $strOrderBy="Order By `dtb_mail`.`Insert` DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>true,
            "fields"=>array(
                "ID"=>"`dtb_mail`.`id`",
                "GroupName"=>"`tbl_type`.`Name`",
                "Insert"=>"`dtb_mail`.`Insert`",
                "Update"=>"`dtb_mail`.`Update`",
                "Delete"=>"`dtb_mail`.`Delete`"
                ),
            "datefields"=>array()
        );
        $this->init($config);
        return $this->jqxBinding();
    }
}
?>
